package cn.itcast.service;

import cn.itcast.dao.OrderSettingDao;
import cn.itcast.pojo.OrderSetting;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *Author:TH
 *Dete:2019/10/2019:51
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;


    //新正预约数据对象
    public void add(List<OrderSetting> orderSettingList) {
        //判断传入的数据是否为空
        if (orderSettingList!=null&&orderSettingList.size()>0){
            //遍历集合
            for (OrderSetting orderSetting : orderSettingList) {
                //判断数据库中是否有该天预约
                long count = orderSettingDao.findByDate(orderSetting.getOrderDate());
                if (count>0){
                    //存在本日预约,修改预约设置
                    orderSettingDao.editOrderSetting(orderSetting);
                }else{
                    //不存在今日预约,增加今日预约
                    orderSettingDao.addOrderSetting(orderSetting);
                }
            }
        }
    }

    //查询输入月份预约信息
    public List<Map> getOrderSettingByMonth(String date) {
        //设置开始时间和结束时间
        String dateBegin = date + "-1";
        String dateEnd = date + "-31";

        Map<String,String> map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> list = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {

            Map orderSettingMap = new HashMap();
            //获得日期（几号）
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());
            //可预约人数
            orderSettingMap.put("number",orderSetting.getNumber());
            //已预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());

            //将map存入集合
            list.add(orderSettingMap);
        }

        return list;
    }

    //修改预约设置
    public void editNumberByDate(OrderSetting orderSetting) {
        //查询当前时间是否有预约
        long count = orderSettingDao.findByDate(orderSetting.getOrderDate());
        if (count>0){
            //存在本日预约,修改预约设置
            orderSettingDao.editOrderSetting(orderSetting);
        }else{
            //不存在今日预约,增加今日预约
            orderSettingDao.addOrderSetting(orderSetting);
        }
    }


}
